package com.youdoneed.friends.coolface;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.friends.model.FaceSearch;
import com.youdoneed.friends.vo.AppSearchResult;
import com.youdoneed.friends.vo.ImgComparisonResult;
import com.youdoneed.user.feign.UserFeignClient;
import com.youdoneed.user.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 人脸相关模块请求控制器
 *
 * @author shum
 * @date 2017.6.14
 */
@RestController
@RequestMapping(value = "/face", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "人脸相关模块请求控制器", description = "人脸相关模块请求控制器")
public class FaceSearchController {

    private static final Logger logger = LoggerFactory.getLogger(FaceSearchController.class);

    @Autowired
    private FaceSearchService faceSearchService;

    @Autowired
    private UserFeignClient userFeignClient;

    @RequestMapping(value = "/getFaceSearchBySearchId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "留言", value = "留言")
    public ResultMessage<FaceSearch> getFaceSearchBySearchId(@ApiParam("searchId") @RequestParam(required = true) String searchId) {
        FaceSearch faceSearch = faceSearchService.getFaceSearchBySearchId(searchId);
        return new ResultMessage<FaceSearch>(faceSearch).toSuccess("留言成功");
    }

    @RequestMapping(value = "/leaveMessage", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "留言", value = "留言")
    public ResultMessage<String> leaveMessage(
            @ApiParam("搜索id") @RequestParam String searchId,
            @ApiParam("留言") @RequestParam(required = false) String msg,
            @ApiParam("经度") @RequestParam(required = false) Double longitude,
            @ApiParam("纬度") @RequestParam(required = false) Double latitude,
            @ApiParam("位置") @RequestParam(required = false) String place) {
        faceSearchService.updateSearch(searchId, msg, longitude, latitude, place);
        return new ResultMessage<String>().toSuccess("留言成功");
    }

    @RequestMapping(value = "/imgComparison", method = RequestMethod.POST)
    @ApiOperation(notes = "图片比对", value = "图片比对")
    public ResultMessage<ImgComparisonResult> imgComparison(
            @ApiParam("新图片地址") @RequestParam String newPathName,
            @RequestHeader("Authorization") String token) {
        logger.info("图片比对");
        String userId = JWTUtils.getTokenUserId(token);
        //当前登录用户详细信息
        User myInfo = userFeignClient.getUser(userId).getData();
        if (StringUtils.hasText(myInfo.getImageUrl())) {
            return new ResultMessage<>(faceSearchService.imgComparison(newPathName, myInfo));
        } else {
            return new ResultMessage<ImgComparisonResult>().toFailure("用户不存在旧的图像！");
        }
    }

    @RequestMapping(value = "/isSameUser", method = RequestMethod.POST)
    @ApiOperation(notes = "图片比对", value = "图片比对")
    public ResultMessage<Boolean> isSameUser(
            @ApiParam("新图片地址") @RequestParam String newPath,
            @ApiParam("旧图片地址") @RequestParam String oldPath) {
        logger.info("图片比对");
        //当前登录用户详细信息
        Boolean isSameUser = faceSearchService.isSameUser(newPath, oldPath);
        return new ResultMessage<Boolean>(isSameUser);
    }

    @RequestMapping(value = "/genImgFeature", method = RequestMethod.POST)
    @ApiOperation(notes = "人脸特征提取", value = "人脸特征提取")
    public ResultMessage<ImgComparisonResult> genImgFeature(
            @ApiParam("图片地址") @RequestParam("pathName") String pathName,
            @RequestHeader("Authorization") String token) {
        if(token.contains(",")){
            token = token.substring(0, token.indexOf(","));
        }
        logger.info("人脸特征提取");
        logger.info("token:"+token);
        String userId = JWTUtils.getTokenUserId(token);
        return new ResultMessage<>(faceSearchService.genImgFeature(pathName, userId));
    }

    /*@RequestMapping(value = "/genImgFeatureByType", method = RequestMethod.POST)
    @ApiOperation(notes = "人脸特征提取", value = "人脸特征提取")
    public ResultMessage<ImgComparisonResult> genImgFeatureByType(
            @ApiParam("图片地址") @RequestParam("pathName") String pathName,
            @ApiParam("儿童id") @RequestParam("childId") String childId,
            @ApiParam("类型，儿童传2")@RequestParam("type") String type) {
        logger.info("儿童特征提取");
        return new ResultMessage<>(faceSearchService.genImgFeatureByType(pathName, childId,type));
    }

    @RequestMapping(value = "/getChildImgFeatureInBatch", method = RequestMethod.POST)
    @ApiOperation(notes = "失踪儿童特征值入库（临时方法）", value = "失踪儿童特征值入库（临时方法）")
    public ResultMessage<?> getChildImgFeatureInBatch(){
        faceSearchService.getChildImgFeatureInBatch();
        return null;
    }*/

    @RequestMapping(value = "/faceSearchUser", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "搜索人脸相似的用户", value = "搜索人脸相似的用户")
    public ResultMessage<AppSearchResult> faceSearchUser(
            @RequestHeader("Authorization") String token,
            @ApiParam("图片地址") @RequestParam(required = false) String pathName,
            @ApiParam("经度") @RequestParam(required = false) Double longitude,
            @ApiParam("纬度") @RequestParam(required = false) Double latitude,
            @ApiParam("位置") @RequestParam(required = false) String place) {
        String myUserId = JWTUtils.getTokenUserId(token);
        return new ResultMessage<>(faceSearchService
                .faceSearchUser(myUserId, pathName, longitude, latitude, place));
    }

    @RequestMapping(value = "/whoCoolfaceMe", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "获取所搜过我的记录", value = "获取所搜过我的记录")
    public ResultMessage<AppSearchResult> whoCoolfaceMe(
            @RequestHeader("Authorization") String token) {
        String myUserId = JWTUtils.getTokenUserId(token);
        return new ResultMessage<>(faceSearchService.whoCoolfaceMe(myUserId));
    }



}
